//
//  HTTPURLConfiguration.m
//  ZhihuDaily
//
//  Created by 梁慧聪 on 16/8/3.
//  Copyright © 2021年 梁慧聪. All rights reserved.
//

#import "CLHTTPURLConfiguration.h"
#import <YYKit/NSArray+YYAdd.h>
#import "NSString+CLUtility.h"
#import "NSDictionary+CLCategory.h"

@interface CLHTTPURLConfiguration ()

@property (nonatomic, copy) NSString *baiduDomain;

@end

@implementation CLHTTPURLConfiguration

SYNTHESIZE_SINGLETON_FOR_CLASS(CLHTTPURLConfiguration)

- (id)init{
    if (self = [super init]) {
        _baiduDomain = @"https://m.baidu.com/su?&from=wise_web&action=opensearch&ie=utf-8&wd=";
    }
    
    return self;
}

- (NSString *)baiduURLWithPath:(NSString *)path{
    if (!path) {
        return nil;
    }
    return [self.baiduDomain stringByAppendingString:[path cl_URLEncodedString]];
}


- (NSString *)urlSearchBookWithPath:(NSString *)keyword {
    if (NULLString(keyword)) {
        return nil;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params cl_setObjectSafe:@"" forKey:@"uid"];
    [params cl_setObjectSafe:[NSString getTimestampSince1970] forKey:@"boxnovelTimeStampNow"];
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data cl_setObjectSafe:@(0) forKey:@"pn"];
    [data cl_setObjectSafe:@(10) forKey:@"rn"];
    [data cl_setObjectSafe:keyword forKey:@"query"];
    [params cl_setObjectSafe:[data cl_toJsonString] forKey:@"data"];
    //https://www.qidian.com/soushu/神秘之劫.html
    //https://m.qidian.com/soushu/%E7%A5%9E%E7%A7%98%E5%B0%BD%E5%A4%B4.html
    return [@"https://appyd.baidu.com/nabook/search/newbookseo" cl_urlSafeAppendParams:params];
}

/*
 书籍封面详细信息
 https://boxnovel.baidu.com/boxnovel/yuedu/wapdetail?fromaction=wise&data=%7B%22doc_id%22%3A%228084dfd9393567ec102de2bd960590c69fc3d802%22%2C%22is_yuedu_source%22%3A1%7D&fr=9
 
 https://boxnovel.baidu.com/boxnovel/yuedu/wapdetail?
 fromaction: wise
 data: {"doc_id":"5c16cfb5b6f8941ea76e58fafab069dc5022470d","is_yuedu_source":1}
 fr: 9
 */
- (NSString *)urlSearchBookDetailWithBook:(CLSearchBook *)book {
    return [self urlSearchBookInfoWithTitle:book.title];
//    return [NSString stringWithFormat:@"https://m.qidian.com/book/%@/catalog",book.novel_book_id];
    //m.qidian.com/book/2984157/0
//    if (NULLString(book.doc_id)) {
//        return nil;
//    }
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params cl_setObjectSafe:@(9) forKey:@"fr"];
//    [params cl_setObjectSafe:@"wise" forKey:@"fromaction"];
//    NSMutableDictionary *data = [NSMutableDictionary dictionary];
////    [data cl_setObjectSafe:@(book.is_yuedu_source) forKey:@"is_yuedu_source"];
//    [data cl_setObjectSafe:book.doc_id forKey:@"doc_id"];
//    [params cl_setObjectSafe:[data cl_toJsonString] forKey:@"data"];
//
//    NSString *result = [@"https://boxnovel.baidu.com/boxnovel/yuedu/wapdetail" cl_urlSafeAppendParams:params];
//    return result;
}

- (NSString *)urlSearchBookInfoWithTitle:(NSString *)title {
    // https://www.baidu.com/s?wd=title%3A%20(笔趣阁%20%22神秘之劫%22%20)&rn=50
    // https://m.baidu.com/s?word=title%3A%20(笔趣阁%20%22神秘之劫%22%20)&rn=50
    NSString *word = [NSString stringWithFormat:@"title: (笔趣阁 \"%@\" )",title];
    return [NSString stringWithFormat:@"https://m.baidu.com/s?word=%@",[word stringByURLEncode]];
}

- (NSString *)urlBookDetailWithBook:(CLBook *)book {
    if (NULLString(book.bid)) {
        return nil;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params cl_setObjectSafe:@(9) forKey:@"fr"];
    [params cl_setObjectSafe:@"wise" forKey:@"fromaction"];
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
//    [data cl_setObjectSafe:@(book.is_yuedu_source) forKey:@"is_yuedu_source"];
    [data cl_setObjectSafe:book.bid forKey:@"doc_id"];
    [params cl_setObjectSafe:[data cl_toJsonString] forKey:@"data"];
    
    NSString *result = [@"https://boxnovel.baidu.com/boxnovel/yuedu/wapdetail" cl_urlSafeAppendParams:params];
    return result;
}

/*https://boxnovel.baidu.com/boxnovel/yuedu/wapcontent?data=%7B%22doc_id%22%3A%225c16cfb5b6f8941ea76e58fafab069dc5022470d%22%2C%22cid%22%3A%224356384946%7C1563006837%22%2C%22is_yuedu_source%22%3A0%7D&fr=9&fromaction=search_jingzhun

 https://boxnovel.baidu.com/boxnovel/yuedu/wapcontent?
 data: {"doc_id":"5c16cfb5b6f8941ea76e58fafab069dc5022470d","cid":"4356384946|1563006837","is_yuedu_source":0}
 fr: 9
 fromaction: search_jingzhun
 
 */
- (NSString *)bookURLWithPath:(NSString *)keyword {
    return @"https://boxnovel.baidu.com/boxnovel/yuedu/wapcontent";
}



@end
